#ifndef RESULTWIDGET_H
#define RESULTWIDGET_H
#include <QDebug>
#include <QWidget>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QCryptographicHash>
#include <QString>
#include "accountmanager.h"
#include "flightjourneywidget.h"
#include <QListWidget>
#include <QListWidgetItem>
#include <QTime>
#include "flightsegmentwidget.h"
#include "flightjourneywidget.h"
#include "journeymodels.h"
#include "algorithm.h"
#include <QStackedWidget>
#include <QWidget>
#include <QLineEdit>
#include <QMessageBox>
#include <QDebug>
#include <QWheelEvent>
#include "journeymodels.h"
#include <QScrollArea>
#include <QWidget>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QVector>
#include <QList>
#include <QString>
#include <QScrollArea>
#include <QTimer>
#include "accountmanager.h"
#include <QDateEdit>
#include "UserProfile.h"

namespace Ui {
class ResultWidget;
}


class ResultWidget : public QWidget
{
    Q_OBJECT

public:
    explicit ResultWidget(QWidget *parent = nullptr);
    ~ResultWidget();
    //void displayResult(const QString& result);
    void displayResults(const QList<FlightJourney>& journeys);
    void displayFlightResults(const QList<FlightJourney>& flightJourneys);
    void debugPrintFlightJourneys(const QList<FlightJourney>& journeys);
    bool saveJourneyToDatabase(const QString& userID, const FlightJourney& journey, const QDate& travelDate);
    bool saveFlightSegments(const FlightJourney& journey);
    bool bookFlight(const QString& userID);
    static void loadAllFlights(const QString& folderPath);


private slots:
    void on_backMain_clicked();
    void on_deCChanged(const QString& text);
    void on_arrCChanged(const QString& text);
    void on_searchAllButton_clicked();
    void on_bookJourneyButton_clicked(int journeyIndex);
    void on_preferBox_currentIndexChanged(int index);

    void on_forUButton_clicked();

private:
    Ui::ResultWidget *ui;
    void clearJourneys();
    QVBoxLayout *journeyResultLayout;
    QLineEdit* arrEdit;
    QLineEdit* deEdit;
    QScrollArea* journeyScrollArea;
    QList<FlightJourney> flightJourneys;
    int currentIndex;  // Track the current index for lazy loading
    void loadNextBatch();  // 加载下一批次航程
    QDateEdit* travelDateEdit;

signals:
    void backMain();
    void bookJourneyRequested(FlightJourneyWidget *journeyWidget);
    void bookingCompleted();

};

#endif // RESULTWIDGET_H
